/**
 *  Copyright 2010 ShenYang voole Co. Ltd.
 *  All right reserved. 
 */
package com.dianning.comm;

import java.awt.*;
import java.io.*;
//import javax.imageio.*;
import magick.CompositeOperator;
import magick.ImageInfo;
import magick.MagickImage;
import magick.PixelPacket;

import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
public class JMagickImgUtil {

	private static final Log logger = LogFactory.getLog(JMagickImgUtil.class);

	public JMagickImgUtil() {
		
	}

	private static void cut(String imgsrc,String imgdist,String imglogo, int widthdist, int heightdist, int widthlogo, int heightlogo){
		try {
			System.setProperty("jmagick.systemclassloader", "no");
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				logger.error(e);
				e.printStackTrace();
			}
			ImageInfo info = new ImageInfo(imgsrc);
			MagickImage image = new MagickImage(new ImageInfo(imgsrc));
			MagickImage logo = new MagickImage(new ImageInfo(imglogo));
			MagickImage scaled = image.scaleImage(widthdist, heightdist);
			scaled.compositeImage(CompositeOperator.AtopCompositeOp, logo, widthlogo, heightlogo);
			scaled.setFileName(imgdist);
			scaled.writeImage(info);
		} catch (Exception ex) {
			logger.error(ex);
		}
	}
	public static void reduceImg(String imgsrc, String imgdist,String imglogo, int widthdist, int heightdist, int widthlogo, int heightlogo) {
		try {
			File srcfile = new File(imgsrc);
			if (!srcfile.exists()) {
				return;
			}
			Image src = javax.imageio.ImageIO.read(srcfile);
			int width = src.getWidth(null);
			int height = src.getHeight(null);
			
			//都小
			
			if (width <= widthdist && height <= heightdist) {
				cut(imgsrc,imgdist,imglogo,width,height,width-widthlogo,height-heightlogo);
				return;
			}
			float wh = (float) widthdist / (float) heightdist;
			if(width <= widthdist){
				float tmp_width = (float) width / wh;
				cut(imgsrc,imgdist,imglogo,(int) tmp_width, heightdist,((int) tmp_width)-widthlogo,heightdist-heightlogo);	
				return;
			}
			if(height <= heightdist){
				float tmp_height = (float) height / wh;
				cut(imgsrc,imgdist,imglogo, width, (int)tmp_height,width -widthlogo,((int) tmp_height)-heightlogo);	
			}
			wh = (float) (width*widthdist) / (float) (height*heightdist);
			// 宽度除以高度的比例
			if (wh > 1) {
				float tmp_height = (float) widthdist / wh;
				cut(imgsrc,imgdist,imglogo,widthdist, (int) tmp_height,widthdist-widthlogo,((int) tmp_height)-heightlogo);	
			} else {	
				float tmp_width = (float) heightdist * wh;
				cut(imgsrc,imgdist,imglogo,(int) tmp_width, heightdist,((int) tmp_width)-widthlogo,heightdist-heightlogo);	
			}
			
		} catch (IOException ex) {
			logger.error(ex);
		}
	}


	private static void cutNoLogo(String imgsrc,String imgdist,int widthdist, int heightdist){
		try {
			System.setProperty("jmagick.systemclassloader", "no");
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				logger.error(e);
				e.printStackTrace();
			}
			ImageInfo info = new ImageInfo(imgsrc);
			MagickImage image = new MagickImage(new ImageInfo(imgsrc));
			MagickImage scaled = image.scaleImage(widthdist, heightdist);
			scaled.setFileName(imgdist);
			scaled.writeImage(info);
		} catch (Exception ex) {
			logger.error(ex);
		}
	}
	public static void reduceImgNoLogo(String imgsrc, String imgdist, int widthdist, int heightdist) {
		try {
			File srcfile = new File(imgsrc);
			if (!srcfile.exists()) {
				return;
			}
			Image src = javax.imageio.ImageIO.read(srcfile);
			int width = src.getWidth(null);
			int height = src.getHeight(null);
			
			//都小
			if (width <= widthdist && height <= heightdist) {
				cutNoLogo(imgsrc,imgdist,width,height);
				return;
			}
			// 宽度除以高度的比例
			float wh = (float) width / (float) height;
			if (wh > 1) {
				float tmp_heigth = (float) widthdist / wh;
				cutNoLogo(imgsrc,imgdist,widthdist, (int) tmp_heigth);
			} else {
				float tmp_width = (float) heightdist * wh;
				cutNoLogo(imgsrc,imgdist,(int) tmp_width, heightdist);
			}
		} catch (IOException ex) {
			logger.error(ex);
		}
	}
}
